package com.base.cn.platform.os.controller.mobile.user.center;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.service.course.agent.config.AgentConfigService;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.user.member.CusUserMemberBuyRecordService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * 个人中心Controller
 *
 * @author s.li
 * @create 2018-06-06-18:48
 */
@Controller
@RequestMapping("/mobile/uc")
public class MobileUserCenterController extends MobileBaseController {
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private AgentConfigService agentConfigService;
    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private CusUserMemberBuyRecordService cusUserMemberBuyRecordService;
    @Autowired
    private CourseCatalogService courseCatalogService;

    /**
     * 个人中心首页
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> coursePlayRecord = coursePlayRecordService.getUserLastPlayCourseRecord(userId);
        TreeMap<String,String> oNoffMap = sysConfigService.getSysConfigContext(ConfigName.ONOFF);
        Map<String,Object> agentConfigMap = agentConfigService.queryAgentConfigList();
        Map<String,Object> agentUserConfig = (Map<String,Object>)((Map<String,Object>) agentConfigMap.get("AGENTCENTER")).get("configMap");
        String agentCenterSwitch = (String)agentUserConfig.get("switch");
        boolean isAgentUser = agentUserService.isAgentUser(userId);
        //设置是否是会员用户
        boolean isMemberUser =  cusUserMemberBuyRecordService.checkUserUseMemberPrice(userId);
        TreeMap<String, String> wechatService = sysConfigService.getSysConfigContext(ConfigName.WECHATSERVICE);
        String serviceSwitch = wechatService.get("serviceSwitch");
        String url = wechatService.get("url");
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        Integer count = courseCatalogService.queryCurrentCount(params);
        request.setAttribute("count", count);
        model.addAttribute("wechatServiceUrl",url);
        model.addAttribute("serviceSwitch",serviceSwitch);
        model.addAttribute("isMemberUser",isMemberUser);
        model.addAttribute("agentCenterSwitch",agentCenterSwitch);
        model.addAttribute("isAgentUser",isAgentUser);
        model.addAttribute("oNoffMap",oNoffMap);
        model.addAttribute("coursePlayRecord",coursePlayRecord);
        model.addAttribute("serviceConfig", sysConfigService.getSysConfigContext(ConfigName.SERVICE));
        return resultPage("/user/center/u-index");
    }

    /**
     * 显示用户详情信息页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/userInfo")
    public String userInfo(HttpServletRequest request, Model model){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> user = cusUserService.userInfo(userId,false,false,false,false,false,false,false);
        model.addAttribute("user",user);
        return resultPage("/user/center/userInfo/user-info");
    }

    /**
     * 系统设置
     * @return
     */
    @RequestMapping("/systemSet")
    public String systemSet(HttpServletRequest request,
                            Model model){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> cusUser = cusUserService.userInfo(userId, false, false, false, false, false, false, false);
        model.addAttribute("isWifi", cusUser.getOrDefault("isWifi", 0));
        return resultPage("/user/center/system/system-set");
    }
}
